﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.Remoting.Messaging;
using System.Runtime.Serialization;
using System.Security.Claims;
using System.Web;

namespace Sharp.Infrastructure
{
    /// <inheritdoc />
    /// <summary>
    /// 通讯上下文
    /// </summary>
    [Serializable]
    //[DebuggerStepThrough]
    public class ContactContext : ILogicalThreadAffinative
    {
        /// <summary>
        /// 关键字
        /// </summary>
        public const string ContextKey = "__contactContext";
        /// <summary>
        /// 头关键字
        /// </summary>
        public const string ContextHeaderLocalName = "ContactContext";
        /// <summary>
        /// 命名空间
        /// </summary>
        public const string ContextHeaderNamespace = "http://www.PlatformSharp.com/";
        /// <summary>
        /// 构造函数
        /// </summary>
        public ContactContext()
        {
        }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="info"></param>
        /// <param name="context"></param>
        protected ContactContext(SerializationInfo info, StreamingContext context)
        {
        }
        /// <summary>
        /// 当前登录的用户
        /// </summary>
        public static ContactContext Current
        {
            get
            {

                if (HttpContext.Current != null && HttpContext.Current.User != null)
                {
                    if (!HttpContext.Current.User.Identity.IsAuthenticated)
                    {
                        //throw new NotSupportedException("尚未登录。");
                        return new ContactContext()
                        {
                            UserId = 0,
                            UserName = ""
                        };
                    }
                    var claimIdentity = HttpContext.Current.User.Identity as ClaimsIdentity;

                    var userId = claimIdentity.FindFirst(ClaimTypes.NameIdentifier);
                    return new ContactContext()
                    {
                        UserId = int.Parse(userId.Value),
                        UserName = claimIdentity.Name
                    };
                }

                if (CallContext.LogicalGetData(ContextKey) == null)
                {
                    CallContext.LogicalSetData(ContextKey, new ContactContext());
                }

                return CallContext.LogicalGetData(ContextKey) as ContactContext;
            }
            //set
            //{
            //    if (HttpContext.Current != null)
            //    {
            //        CallContext.LogicalSetData(ContextKey, value);
            //        HttpContext.Current.Session[ContextKey] = value;
            //    }
            //    else
            //    {
            //        CallContext.LogicalSetData(ContextKey, value);
            //    }
            //}
        }
        /// <summary>
        /// 用户Id
        /// </summary>
        public int UserId
        {
            get;
            set;
        }

        /// <summary>
        /// 用户名
        /// </summary>
        public string UserName
        {
            get;
            set;
        }

        /// <summary>
        /// Saas组织Id
        /// </summary>
        public Guid? OrgId
        {
            get;
            set;
        }
        /// <summary>
        /// 清空
        /// </summary>
        public static void Clear()
        {
            //Current = null;
        }
    }
}